package com.zhkj.txg.module.product.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.text.buildSpannedString
import androidx.core.text.color
import androidx.core.text.scale
import com.zhkj.txg.R
import razerdp.basepopup.BasePopupWindow

class ProductSharePopupWindow(
    context: Context,
    private val onClick: (type: Int) -> Unit
) : BasePopupWindow(context) {

    private var tvShareTitle: TextView? = null
    private var tvShareContent: TextView? = null
    private var frame_qrcode: FrameLayout? = null

    var productName: String = ""
    var productImage: MutableList<String> = mutableListOf()
    var productPrice: String = ""
    var shareUrl: String = ""
    var goodsId: Long = 0

    override fun onCreateContentView(): View = createPopupById(R.layout.popup_product_share)
    override fun onCreateShowAnimation(): Animation = getTranslateVerticalAnimation(1f, 0f, 300)
    override fun onCreateDismissAnimation(): Animation = getTranslateVerticalAnimation(0f, 1f, 300)
    override fun onViewCreated(contentView: View?) {
        contentView?.let {
            frame_qrcode = it.findViewById(R.id.frame_qrcode)
            tvShareTitle = it.findViewById(R.id.tvShareTitle)
            tvShareContent = it.findViewById(R.id.tvShareContent)
            // 微信分享
            it.findViewById<View>(R.id.tvShareWeChat).setOnClickListener {
                onClick(0)
                dismiss()
            }
            // 微信朋友圈分享
            it.findViewById<View>(R.id.tvShareCircle).setOnClickListener {
                onClick(1)
                dismiss()
            }
            // 会员分享
            it.findViewById<View>(R.id.tvShareQRCode).setOnClickListener {
                onClick(2)
                dismiss()
            }
            // 弹窗取消
            it.findViewById<View>(R.id.btnShareDismiss).setOnClickListener { dismiss() }
        }
    }

    fun setShareEarn(shareEarn: String) {
        tvShareTitle?.text = buildSpannedString {
            append("分享赚")
            color(ContextCompat.getColor(context, R.color.colorMain)) { scale(1.3f) { append(shareEarn) } }
            append("元哦")
        }
        tvShareContent?.text = String.format(context.getString(R.string.share_earn_hint), shareEarn)
    }

    fun setQRcode(visibility: Int) {
        frame_qrcode?.visibility = visibility
    }
}